<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="renovateRule" :model="renovateForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="renovateForm.title" />
      </el-form-item>
      <el-form-item label="类型">
        <el-radio v-model="renovateForm.style.value" label="1">现金卷</el-radio>
        <el-radio v-model="renovateForm.style.value" label="2">折扣卷</el-radio>
      </el-form-item>
      <el-form-item label="金额">
        <el-input v-model="renovateForm.discount" />
      </el-form-item>
      <el-form-item label="发行规模">
        <el-input v-model="renovateForm.quantity" />
      </el-form-item>
      <el-form-item label="有效期">
        <el-date-picker 
          v-model="renovateForm.begin_time"
          type="datetime"
          :shortcuts="shortcuts"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="开始日期"
          align="right"
        />
        <el-date-picker 
          v-model="renovateForm.end_time"
          type="datetime"
          :shortcuts="shortcuts"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="开始日期"
          align="right"
        />
      </el-form-item>
      <el-form-item label="规则">
        <el-input v-model="renovateForm.content" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="优惠卷规则" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="renovateForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="renovateForm.status.value === 5" type="danger" @click="handleDelete(temp)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveRenovate, removeRenovateById, getRenovate } from '@/api/program/renovate'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      renovateForm: {
        title: '',
        discount: '',
        status: {
          label: '',
          value: 1
        },
        style: {
          label: '',
          value: 1
        },
        begin_time: '',
        end_time: '',
        quantity: ''
      },
      renovateRule: []
    }
  },
  created() {
    let renovateId = this.$route.query && this.$route.query.renovateId
    if (renovateId !== undefined) {
      renovateId = parseInt(renovateId)
      if (!isNaN(renovateId)) {
        this.listLoading = true
        const formData = {
          'renovateId': renovateId
        }
        getRenovate(formData).then(response => {
          if (response.status === 200) {
            this.renovateForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(renovateId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'renovate' })
    },
    createData() {
      const _renovateForm = this.renovateForm
      _renovateForm.status = _renovateForm.status.value
      _renovateForm.style = _renovateForm.style.value
      if (_renovateForm.identity > 0) {
        _renovateForm.renovateId = _renovateForm.identity
      }
      saveRenovate(_renovateForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'renovate' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const _this = this
      const formData = {
        'renovateId': row.identity
      }
      console.log(index)
      removeRenovateById(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          _this.$router.push({ path: 'renovate' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
